package com.mDigitalHealth.http;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

import org.json.simple.JSONArray;
import org.json.simple.JSONValue;
import org.json.simple.parser.JSONParser;

public class DigitalHealthUploadStepInfoRequest {
	private String _Transaction_Id;
	private String _Login_Name;
	private String _User_Number;
	private Hour_Step_Count[] _Hour_Step_Count;

	public DigitalHealthUploadStepInfoRequest() {
		this._Transaction_Id = "";
		this._Login_Name = "";
		this._User_Number = "";
	}

	public DigitalHealthUploadStepInfoRequest(String strTransactionId, String strLoginName,
			String strUserNumber, Hour_Step_Count[] hourStepCount) {
		this._Transaction_Id = strTransactionId;
		this._Login_Name = strLoginName;
		this._User_Number = strUserNumber;
		this._Hour_Step_Count = hourStepCount;
	}

	public void setTransactionId(String strTransactionId) {
		this._Transaction_Id = strTransactionId;
	}

	public String getTransactionId() {
		return this._Transaction_Id;
	}

	public void setLoginName(String strLoginName) {
		this._Login_Name = strLoginName;
	}

	public String getLoginName() {
		return this._Login_Name;
	}

	public void setUserNumber(String strUserNumber) {
		this._User_Number = strUserNumber;
	}

	public String getUserNumber() {
		return this._User_Number;
	}

	public void setHourStepCount(Hour_Step_Count[] stepCount) {
		this._Hour_Step_Count = stepCount;
	}

	public Hour_Step_Count[] getHourStepCount() {
		return this._Hour_Step_Count;
	}

	public String encode() {
		try {
			LinkedHashMap map = new LinkedHashMap();

			map.put("Transaction_Id", this._Transaction_Id);
			map.put("Login_Name", this._Login_Name);
			map.put("User_Number", this._User_Number);

			LinkedList lst = new LinkedList();
			LinkedHashMap mapCount = null;

			for (int i = 0; i < this._Hour_Step_Count.length; i++) {
				mapCount = new LinkedHashMap();
				mapCount.put("Hour_Stamp", this._Hour_Step_Count[i]._Hour_Stamp);
				mapCount.put("Hour_Step_Count_Total",
						Integer.valueOf(this._Hour_Step_Count[i]._Hour_Step_Count_Total));
				mapCount.put("Hour_Step_Count_High",
						Integer.valueOf(this._Hour_Step_Count[i]._Hour_Step_Count_High));
				mapCount.put("Hour_Step_Count_Mid",
						Integer.valueOf(this._Hour_Step_Count[i]._Hour_Step_Count_Mid));
				mapCount.put("Hour_Step_Count_Low",
						Integer.valueOf(this._Hour_Step_Count[i]._Hour_Step_Count_Low));

				lst.add(mapCount);
			}
			map.put("Hour_Step_Count", lst);
			return JSONValue.toJSONString(map);
		} catch (Exception e) {
		}
		return null;
	}

	public int decode(String strCode) {
		try {
			if (strCode == null) {
				return -1;
			}
			JSONParser parser = new JSONParser();
			Map map = (Map) parser.parse(strCode);
			this._Transaction_Id = ((String) map.get("Transaction_Id"));
			this._Login_Name = ((String) map.get("Login_Name"));
			this._User_Number = ((String) map.get("User_Number"));

			Map mapCount = null;
			JSONArray array = (JSONArray) map.get("Hour_Step_Count");
			int nSize = array.size();
			Hour_Step_Count[] stepCountSet = new Hour_Step_Count[nSize];
			for (int i = 0; i < nSize; i++) {
				Hour_Step_Count stepCount = new Hour_Step_Count();
				mapCount = (Map) array.get(i);
				stepCount._Hour_Stamp = ((String) mapCount.get("Hour_Stamp"));
				stepCount._Hour_Step_Count_Total = Integer.parseInt(mapCount.get(
						"Hour_Step_Count_Total").toString());
				stepCount._Hour_Step_Count_High = Integer.parseInt(mapCount.get(
						"Hour_Step_Count_High").toString());
				stepCount._Hour_Step_Count_Mid = Integer.parseInt(mapCount.get(
						"Hour_Step_Count_Mid").toString());
				stepCount._Hour_Step_Count_Low = Integer.parseInt(mapCount.get(
						"Hour_Step_Count_Low").toString());
				stepCountSet[i] = stepCount;
			}
			setHourStepCount(stepCountSet);

			return 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
}